package main

import (
	"flag"
	"fmt"
	"oss-uploader/app/controller"
	"oss-uploader/app/service"
	"strconv"
)

func main() {
	tips()
	cmd()
}

func tips() {
	fmt.Println("用法:")
	fmt.Println("\t" + `oss_uploader.exe -type="类型" -filepath="本地文件路径" -prefix="上传到oss空间的指定目录" [-worknum="工作协程数"]`)
	fmt.Println("示例:")
	fmt.Println("\t" + `oss_uploader.exe -type="qiniu" -filepath="C:\Program Files" -prefix="v1" [-worknum="5"]`)
	fmt.Println("参数说明:")
	fmt.Println("\ttype: 类型,可选值qiniu或者ali,可以上传到七牛云(qiniu)或者阿里云(ali)的oss空间")
	fmt.Println("\tfilepath: 本地文件路径,可以是文件或者目录")
	fmt.Println("\tprefix: 上传到oss空间的指定目录,若prefix值为空,则对应oss空间的根目录")
	fmt.Println("\tworknum: 可选参数,工作协程数,默认值为5,范围5-10000")
	fmt.Println("")
}

func cmd() {
	var workNum string
	flag.StringVar(&service.OssType, "type", "", "please input qiniu or ali")
	flag.StringVar(&service.UploadRootPath, "filepath", "", "please input filepath")
	flag.StringVar(&service.DestPathPrefix, "prefix", "", "please input oss prefix")
	flag.StringVar(&workNum, "worknum", "5", "please input worknum,minimum value is 5")
	flag.Parse()

	//fmt.Println(service.OssType, service.UploadRootPath, service.DestPathPrefix, workNum)
	if !(service.OssType == "qiniu" || service.OssType == "ali") {
		panic("type is error")
	}

	if len(service.UploadRootPath) == 0 {
		panic("filepath is error")
	}

	workNum64, _ := strconv.ParseInt(workNum, 10, 32)
	if workNum64 <= 0 {
		workNum64 = 5
	}
	service.WorkNum = int(workNum64)

	fmt.Println(">>> upload start...")
	if service.OssType == "qiniu" {
		controller.QiniuWork()
	} else if service.OssType == "ali" {
		controller.AliWork()
	}
	fmt.Println(">>> upload over...")
}
